#ifndef CDN_KEY_H
#define CDN_KEY_H
/*
*******************************************************************************
** Copyright (C), 2014-2020, TONLY AV R&D CENTER SOFTWARE DEVELOPMENT DEPARTMENT
** FileName: 
** <author>:             
** Date:
** Description:
**
** Others:
** Function List:
**   1.
** History:
**  <Author>    <date>    <content>    <version>
*******************************************************************************
*/
//*****************************************************************************
// File Include
//*****************************************************************************
#include "stdint.h"
#include "light_input_system.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_gpio.h"

//*****************************************************************************
// Local Defines
//*****************************************************************************
//按键号定义
#define KEY_ID_NULL  								0
#define KEY_ID_1     								1

// KEY1
#define CDN_KEY1_PORT               gpioPortB
#define CDN_KEY1_PIN                0

#define KEY_NUM_CNT    							1  

//按键信息
#define KEY_POLLING_TIME    				20  //按键程序扫描时间，单位ms
#define KEY_HOLD_COUNT      				10  //按下保持时间
#define DEBOUNCE_COUNT    					1   //消抖间隔，1个按键扫描周期
#define KEY_REPEAT_CLICK_INTERVAL   25  //双击间隔时间，正常值为30(KEY_POLLING_TIME*30 = 600ms)

#define LONG_COUNT_300MS       			((300/KEY_POLLING_TIME) * 1) //300MS时间计数
#define LONG_COUNT_400MS       			((400/KEY_POLLING_TIME) * 1) //400MS时间计数
#define LONG_COUNT_1S     					((1000/KEY_POLLING_TIME) * 1) //1S时间计数

#define LONG_DOWN_TIME_1S						(1*LONG_COUNT_1S) //长按1S
#define LONG_DOWN_TIME_3S       		(3*LONG_COUNT_1S) //长按3S
#define LONG_DOWN_TIME_4S       		(4*LONG_COUNT_1S) //长按4S
#define LONG_DOWN_TIME_5S						(5*LONG_COUNT_1S) //长按5S
#define LONG_LONG_DOWN_TIME_10S  		(10*LONG_COUNT_1S) //长按10S

//*****************************************************************************
// Local enums
//*****************************************************************************
//按键行为结果
typedef enum
{
  KEY_RESULT_Idle,
  KEY_RESULT_CLICK,
  KEY_RESULT_CLICK_UP,
  KEY_RESULT_REPEAT_CLICK,
  KEY_RESULT_LONG_DOWN,
  KEY_RESULT_LONG_DOWN_UP,
  KEY_RESULT_LONG_DOWN_5S,
  KEY_RESULT_LONG_DOWN_5S_UP,
  KEY_RESULT_LONG_LONG_DOWN,
  KEY_RESULT_LONG_LONG_DOWN_UP,
}KeyDownResult;

//按键动作状态
typedef enum
{
  KEY_STATE_IDLE,
  KEY_STATE_PRESS,
  KEY_STATE_HOLD,
  KEY_STATE_RELEASE,
}KeyActionState;

//*****************************************************************************
// Local structures
//*****************************************************************************
//按键动作按下或者松开
typedef struct KeyActionDownUp
{
  uint8_t id;
  uint16_t down;
  uint16_t up;
}KeyActionDownUp;

//按键动作相关信息
typedef struct KeyActionInfo
{
  uint8_t click_time;
  uint8_t repeatCnt;
  uint8_t preId;
  KeyActionDownUp key;
  KeyActionState state;
  KeyDownResult result;
}KeyActionInfo;

//按键信息
typedef struct KeyInfo
{
  uint8_t KeyID;           //按键索引ID
  uint8_t result;          //按键动作
}KeyInfo;


//按键输入
typedef struct KeyInputEvent {
  INPUT_EVENT_TYPE eType;     //设备类型
  KeyInfo iKey;               //按键
}KeyInputEvent, *PKeyInputEvent;

//*****************************************************************************
// Local function declaration
//*****************************************************************************
void key_scan(void);
void KeyGetValuePutEventBuff(void);
void KeyEventClickHandler(PKeyInputEvent KeyEvent);
void KeyFactoryResetControlHandle(void);
void KeyFactoryTestInto(void);

#endif
